package com.example.shellshopping.utils;

import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.Map;
import java.util.Set;

public class BaseActivity extends AppCompatActivity implements Callback.CommonCallback<String>{
    private String TAG = "animee123";
    public void postNetwork(String url, Map<String,String> map){
        RequestParams params = new RequestParams(url);
        //将提交的键值对放到请求参数当中
        Set<String> keySet = map.keySet();
        for(String key:keySet){
            String val = map.get(key);
            params.addParameter(key,val);
        }
        params.setAsJsonContent(true);
        params.setBodyContentType("application/json;charset=utf-8");
        x.http().post(params,this);
    }

    /*public void getNetwork(String url){
        RequestParams params = new RequestParams(url);
        x.http().get(params,this);
    }*/
    public void getNetwork(String url, Map<String,String> map){
        RequestParams params = new RequestParams(url);
        //将提交的键值对放到请求参数当中
        Set<String> keySet = map.keySet();
        for(String key:keySet){
            String val = map.get(key);
            params.addParameter(key,val);
        }
        params.setAsJsonContent(true);
        params.setBodyContentType("application/json;charset=utf-8");
        x.http().get(params,this);
    }

    public void getNetwork(String url){
        RequestParams params = new RequestParams(url);
        x.http().get(params,this);
    }

    @Override  //获取数据成功时，会调用的方法
    public void onSuccess(String result) {
        Log.i(TAG,"onSuccess:result=="+result);
    }

    @Override  //获取数据失败时，会调用的方法
    public void onError(Throwable ex, boolean isOnCallback) {
        Log.i(TAG,"onError:ex=="+ex.getMessage());
    }

    @Override
    public void onCancelled(Callback.CancelledException cex) {

    }

    @Override
    public void onFinished() {

    }
}
